package com.asm.service;

import com.asm.dto.PageResult;
import com.asm.entity.sys.Role;
import com.asm.repository.RoleRepository;
import com.asm.util.SnowflakeIdWorker;
import com.asm.vo.sys.RoleVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 功能描述
 *
 * @param * @param null
 * @author Tanzujie
 * @date 2019-07-10
 * @return
 */
@Service
public class RoleService {
    @Autowired
    private RoleRepository roleRepository;
    @Autowired
    private SnowflakeIdWorker snowflakeIdWorker;

    /**
     * 获取角色列表
     *
     * @param page
     * @return
     */
    private List<RoleVo> getRoleVoList(Page<Role> page) {
        List<RoleVo> list = new ArrayList<>();
        for (Role Role : page.getContent()) {
            RoleVo roleVo = new RoleVo();
            BeanUtils.copyProperties(Role, roleVo);
            list.add(roleVo);
        }
        return list;
    }

    /**
     * 分页查询所有角色
     *
     * @param pageNumber
     * @param pageSize
     * @return
     */
    public PageResult<RoleVo> findAllRoles(Integer pageNumber, Integer pageSize) {
        Page<Role> page = roleRepository.findAll(PageRequest.of(pageNumber, pageSize));
        return new PageResult<RoleVo>(pageNumber, pageSize, page.getTotalPages(), getRoleVoList(page));
    }

    /**
     * 根据id查询角色
     *
     * @param id
     * @return
     */
    public RoleVo findRoleById(Long id) {
        Role Role = roleRepository.findById(id).get();
        RoleVo roleVo = new RoleVo();
        BeanUtils.copyProperties(Role, roleVo);
        return roleVo;
    }

    /**
     * 根据角色类型分页查询角色
     *
     * @param code
     * @param pageNumber
     * @param pageSize
     * @return
     */
    public PageResult<RoleVo> findRoleByCode(String code, Integer pageNumber, Integer pageSize) {
        Pageable page = PageRequest.of(pageNumber, pageSize);
        Page<Role> roles = roleRepository.findByCode(code, page);
        return new PageResult<RoleVo>(pageNumber, pageSize, roles.getTotalPages(), getRoleVoList(roles));
    }

    /**
     * 添加角色
     *
     * @param roleVo
     */
    @Transactional(rollbackFor = Exception.class)
    public void addRole(RoleVo roleVo) {
        Role Role = new Role();
        BeanUtils.copyProperties(roleVo, Role);
        Role.setId(snowflakeIdWorker.nextId());
        Role.setCreateBy(1L);
        Role.setCreateAt(new Date());
        Role.setUpdateBy(1L);
        Role.setUpdateAt(new Date());
        roleRepository.save(Role);
    }

    /**
     * 更新角色
     *
     * @param roleVo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateRole(RoleVo roleVo) {
        Role Role = roleRepository.findById(roleVo.getId()).get();
        BeanUtils.copyProperties(roleVo, Role);
        Role.setUpdateBy(1L);
        Role.setUpdateAt(new Date());
        roleRepository.save(Role);
    }

    /**
     * 根据Id删除角色
     *
     * @param id
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteRole(Long id) {
        roleRepository.deleteById(id);
    }
}
